﻿#ifndef ANALYZER_WORKER_H
#define ANALYZER_WORKER_H
#include <thread>
#include <queue>
#include <mutex>
#include <opencv2/opencv.hpp>
#include "ft2build.h"
#include "freetype/freetype.h"


namespace AVSAnalyzer {
	class Scheduler;
	class AvPullStream;
	class AvPushStream;
	class Analyzer;
	struct Control;
	struct Frame;
	class FramePool;
	class GenerateAlarmImage;

	class Worker
	{
	public:
		explicit Worker(Scheduler* scheduler, Control* control);
		~Worker();
	public:
		static void decodeVideoThread(void* arg);		// （线程）解码视频帧和实时分析视频帧
		void handleDecodeVideo();
		static void generateAlarmThread(void* arg);		// （线程）实时准备报警视频帧
		void handleGenerateAlarm();
	public:
		bool start(std::string& msg);

		bool getState();
		void remove();
	public:
		Control* mControl;
		Scheduler* mScheduler;
		AvPullStream* mPullStream;
		AvPushStream* mPushStream;
		Analyzer*  mAnalyzer;
		FramePool* mVideoFramePool;
		bool mFtValid = false;
		std::vector<std::thread*> mThreads;
		bool mState = false;
	public:
		// 在Worker类的public或private部分添加或修改此方法声明
		void drawChineseText(cv::Mat& img, const std::string& text, int x, int y, cv::Scalar color);
		void calculateTextSize(const std::string& text, int& width, int& height);
	private:
		//报警视频帧
		std::queue <Frame*> mAlarmVideoFrameQ;
		std::mutex          mAlarmVideoFrameQ_mtx;		
		GenerateAlarmImage* mAlarmImageGenerator;

		FT_Library mFtLibrary;
    	FT_Face mFtFace;  
	private:
		void addAlarmVideoFrameQ(Frame* frame);
		int  getAlarmVideoFrameQSize();
		bool getAlarmVideoFrame(Frame*& frame);
		void clearAlarmVideoFrameQ();
	};
}
#endif //ANALYZER_WORKER_H